/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Identificationqualifier;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Identificationelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class IdentificationqualifierJpaController {

    public IdentificationqualifierJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Identificationqualifier save(Identificationqualifier identificationqualifier) {
        Identificationqualifier aux = findIdentificationqualifier(identificationqualifier.getIdentificationqualifier());
        if(aux==null){
            return create(identificationqualifier);
        } else
            return aux;
    }

    public Identificationqualifier create(Identificationqualifier identificationqualifier) {
        Identificationqualifier r = null;
        if (identificationqualifier.getIdentificationelementList() == null) {
            identificationqualifier.setIdentificationelementList(new ArrayList<Identificationelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Identificationelement> attachedIdentificationelementList = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListIdentificationelementToAttach : identificationqualifier.getIdentificationelementList()) {
                identificationelementListIdentificationelementToAttach = em.getReference(identificationelementListIdentificationelementToAttach.getClass(), identificationelementListIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementList.add(identificationelementListIdentificationelementToAttach);
            }
            identificationqualifier.setIdentificationelementList(attachedIdentificationelementList);
            r = em.merge(identificationqualifier);
            for (Identificationelement identificationelementListIdentificationelement : identificationqualifier.getIdentificationelementList()) {
                Identificationqualifier oldIdentificationqualifierOfIdentificationelementListIdentificationelement = identificationelementListIdentificationelement.getIdentificationqualifier();
                identificationelementListIdentificationelement.setIdentificationqualifier(identificationqualifier);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
                if (oldIdentificationqualifierOfIdentificationelementListIdentificationelement != null) {
                    oldIdentificationqualifierOfIdentificationelementListIdentificationelement.getIdentificationelementList().remove(identificationelementListIdentificationelement);
                    oldIdentificationqualifierOfIdentificationelementListIdentificationelement = em.merge(oldIdentificationqualifierOfIdentificationelementListIdentificationelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Identificationqualifier identificationqualifier) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identificationqualifier persistentIdentificationqualifier = em.find(Identificationqualifier.class, identificationqualifier.getIdidentificationqualifier());
            List<Identificationelement> identificationelementListOld = persistentIdentificationqualifier.getIdentificationelementList();
            List<Identificationelement> identificationelementListNew = identificationqualifier.getIdentificationelementList();
            List<Identificationelement> attachedIdentificationelementListNew = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListNewIdentificationelementToAttach : identificationelementListNew) {
                identificationelementListNewIdentificationelementToAttach = em.getReference(identificationelementListNewIdentificationelementToAttach.getClass(), identificationelementListNewIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementListNew.add(identificationelementListNewIdentificationelementToAttach);
            }
            identificationelementListNew = attachedIdentificationelementListNew;
            identificationqualifier.setIdentificationelementList(identificationelementListNew);
            identificationqualifier = em.merge(identificationqualifier);
            for (Identificationelement identificationelementListOldIdentificationelement : identificationelementListOld) {
                if (!identificationelementListNew.contains(identificationelementListOldIdentificationelement)) {
                    identificationelementListOldIdentificationelement.setIdentificationqualifier(null);
                    identificationelementListOldIdentificationelement = em.merge(identificationelementListOldIdentificationelement);
                }
            }
            for (Identificationelement identificationelementListNewIdentificationelement : identificationelementListNew) {
                if (!identificationelementListOld.contains(identificationelementListNewIdentificationelement)) {
                    Identificationqualifier oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement = identificationelementListNewIdentificationelement.getIdentificationqualifier();
                    identificationelementListNewIdentificationelement.setIdentificationqualifier(identificationqualifier);
                    identificationelementListNewIdentificationelement = em.merge(identificationelementListNewIdentificationelement);
                    if (oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement != null && !oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement.equals(identificationqualifier)) {
                        oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement.getIdentificationelementList().remove(identificationelementListNewIdentificationelement);
                        oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement = em.merge(oldIdentificationqualifierOfIdentificationelementListNewIdentificationelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = identificationqualifier.getIdidentificationqualifier();
                if (findIdentificationqualifier(id) == null) {
                    throw new NonexistentEntityException("The identificationqualifier with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identificationqualifier identificationqualifier;
            try {
                identificationqualifier = em.getReference(Identificationqualifier.class, id);
                identificationqualifier.getIdidentificationqualifier();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The identificationqualifier with id " + id + " no longer exists.", enfe);
            }
            List<Identificationelement> identificationelementList = identificationqualifier.getIdentificationelementList();
            for (Identificationelement identificationelementListIdentificationelement : identificationelementList) {
                identificationelementListIdentificationelement.setIdentificationqualifier(null);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
            }
            em.remove(identificationqualifier);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Identificationqualifier> findIdentificationqualifierEntities() {
        return findIdentificationqualifierEntities(true, -1, -1);
    }

    public List<Identificationqualifier> findIdentificationqualifierEntities(int maxResults, int firstResult) {
        return findIdentificationqualifierEntities(false, maxResults, firstResult);
    }

    private List<Identificationqualifier> findIdentificationqualifierEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Identificationqualifier.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Identificationqualifier findIdentificationqualifier(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Identificationqualifier.class, id);
        } finally {
            em.close();
        }
    }

    public Identificationqualifier findIdentificationqualifier(String q) {
        Query query= getEntityManager().createNamedQuery("Identificationqualifier.findByIdentificationqualifier");
        query.setParameter("identificationqualifier",q);

        return query.getResultList().isEmpty()?null:(Identificationqualifier)query.getResultList().get(0);
    }

    public int getIdentificationqualifierCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Identificationqualifier> rt = cq.from(Identificationqualifier.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
